import Vue from 'vue'
import Router from 'vue-router'
// import Login from "@/components/login/login"
Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    { path: '/',
      component: (resolve) => require(['@/components/login/login'], resolve)
    },
    { path: '/dashboard', 
      redirect: '/dashboard',
      component: (resolve) => require(['@/layout/layout'], resolve),
      meta: { requireAuth: true },
      children: [
        {
          path: '',
          name: '首页',
          component: (resolve) => require(['@/components/dashboard/dashboard'], resolve)
        }
      ]
    },
    { path: '/carriers', 
      redirect: '/carriers',
      component: (resolve) => require(['@/layout/layout'], resolve),
      meta: { requireAuth: true },
      children: [
        {
          path: '',
          name: '运营商管理',
          component: (resolve) => require(['@/components/carrier/index'], resolve),
        },
        {
          path: ':id1(\\D+)',
          name: 'carrierform',
          component: (resolve) => require(['@/components/carrier/form'], resolve)
        },
        {
          path: ':id2(\\d+)',
          name: '',
          component: (resolve) => require(['@/components/carrierDetail/carrierDetail'], resolve),
          children: [
            {
              path: '/',
              name: 'index',
              component: (resolve) => require(['@/components/carrierShow/index'], resolve)
            },
            {
              path: 'china_telecom_api_accounts',
              component: (resolve) => require(['@/components/apiLayout/apiLayout'], resolve),
               children: [
                  {
                    path: '',
                    name: 'china_telecom_api_accounts',
                    component: (resolve) => require(['@/components/china_telecom_api_accounts/index'], resolve)
                  },
                  {
                    path: ':id3(\\D+)',
                    name: 'apiform',
                    component: (resolve) => require(['@/components/china_telecom_api_accounts/form'], resolve)
                  }
               ]
            },
            {
              path: 'carrier_product',
              component: (resolve) => require(['@/components/apiLayout/apiLayout'], resolve),
              children: [
                {
                  path: '',
                  name: 'carrier_product',
                  component: (resolve) => require(['@/components/carrier_product/index'], resolve)
                },
                {
                  path: ':id4(\\D{3,4})',
                  name: 'productform',
                  component: (resolve) => require(['@/components/carrier_product/form'], resolve)
                },
                {
                  path: ':id5(\\D{5,})',
                  name: 'carrier_product_price',
                  component: (resolve) => require(['@/components/carrier_product/carrier_product_price'], resolve)
                }
              ]
            },
            {
              path: 'sim_cards',
              component: (resolve) => require(['@/components/apiLayout/apiLayout'], resolve),
              children: [
                {
                  path: '/',
                  name: 'sim_cards',
                  component: (resolve) => require(['@/components/sim_cards/index'], resolve)
                },
                {
                  path: ':id6(\\d+)',
                  name: '',
                  component: (resolve) => require(['@/components/sim_cards/show'], resolve),
                  children: [
                    {
                      path: '',
                      name: 'usagedetail',
                      component: (resolve) => require(['@/components/sim_cards/usage_detail'], resolve),
                    },
                    {
                      path: 'smsusagedetail',
                      name: 'smsusagedetail',
                      component: (resolve) => require(['@/components/sim_cards/sms_usage_detail'], resolve),
                    },
                    {
                      path: 'realnamecertification',
                      name: 'realnamecertification',
                      component: (resolve) => require(['@/components/sim_cards/real_name_certification'], resolve),
                    },
                    {
                      path: 'historyevent',
                      name: 'historyevent',
                      component: (resolve) => require(['@/components/sim_cards/history_event'], resolve),
                    },{
                      path: 'package',
                      name: 'package',
                      component: (resolve) => require(['@/components/sim_cards/package'], resolve),
                    }
                  ]
                }
              ]
            },
            {
              path: 'carrier_order',
              name: 'carrier_order',
              component: (resolve) => require(['@/components/carrier_order/index'], resolve)
            }
          ]
        }
      ]
    },
    {
      path: '/agent_profit',
      component: (resolve) => require(['@/layout/layout'], resolve),
      meta: { requireAuth: true },
      children: [
        {
          path: '',
          component: (resolve) => require(['@/components/agentDetail/index'], resolve),
          children: [
            {
              path:'',
              component: (resolve) => require(['@/components/agentLayout/agentLayout'], resolve),
              children: [
                {
                  path: '',
                  name: 'index',
                  component: (resolve) => require(['@/components/agent/agent_profit/index'], resolve)
                },
                {
                  path: ':id7(\\D{3,4})',
                  name: 'agentform',
                  component: (resolve) => require(['@/components/agent/agent_profit/form'], resolve)
                }
              ]
            },
            {
              path:'agent_product_template',
              component: (resolve) => require(['@/components/agentLayout/agentLayout'], resolve),
              children: [
                {
                  path: '',
                  name: 'agent_product_template',
                  component: (resolve) => require(['@/components/agent/agent_product_template/index'], resolve)
                },
                {
                  path: ':id8(\\D+)',
                  name: 'agentproductform',
                  component: (resolve) => require(['@/components/agent/agent_product_template/form'], resolve)
                } 
              ]
            }
          ]
        },
        {
          path: ':id9(\\d+)',
          name: '',
          component: (resolve) => require(['@/components/agent/agent_detail/index'], resolve),
          children: [
            {
              path: '/',
              name: 'index',
              component: (resolve) => require(['@/components/agent/agentShow/index'], resolve)
            },
            {
              path: 'terminal_products',
              component: (resolve) => require(['@/components/agent/agentDetailLayout/index'], resolve),
               children: [
                  {
                    path: '',
                    name: 'terminal_products',
                    component: (resolve) => require(['@/components/agent/terminal_products/index'], resolve)
                  },
                  {
                    path: ':id10(\\D{3,4})',
                    name: 'terminal_products/agentproductform',
                    component: (resolve) => require(['@/components/agent/agent_product_template/form'], resolve)
                  },
                  {
                    path: ':id11(\\D{5,})',
                    name: 'terminal_products/carrier_product_price',
                    component: (resolve) => require(['@/components/carrier_product/carrier_product_price'], resolve)
                  }
               ]
            },
            {
              path: 'sim_cards',
              component: (resolve) => require(['@/components/agent/agentDetailLayout/index'], resolve),
               children: [
                  {
                    path: '',
                    name: 'sim_cards',
                    component: (resolve) => require(['@/components/agent/sim_cards/index'], resolve)
                  },
                  {
                    path: ':id6(\\d+)',
                    name: '',
                    component: (resolve) => require(['@/components/sim_cards/show'], resolve),
                    children: [
                      {
                        path: '',
                        name: 'usagedetail',
                        component: (resolve) => require(['@/components/sim_cards/usage_detail'], resolve)
                      },
                      {
                        path: 'smsusagedetail',
                        name: 'smsusagedetail',
                        component: (resolve) => require(['@/components/sim_cards/sms_usage_detail'], resolve)
                      },
                      {
                        path: 'realnamecertification',
                        name: 'realnamecertification',
                        component: (resolve) => require(['@/components/sim_cards/real_name_certification'], resolve)
                      },
                      {
                        path: 'historyevent',
                        name: 'historyevent',
                        component: (resolve) => require(['@/components/sim_cards/history_event'], resolve)
                      },{
                        path: 'package',
                        name: 'package',
                        component: (resolve) => require(['@/components/sim_cards/package'], resolve)
                      }
                    ]
                  }
               ]
            },
            {
              path: 'agent_order_statistics',
              component: (resolve) => require(['@/components/agent/agentDetailLayout/index'], resolve),
               children: [
                  {
                    path: '',
                    name: 'agent_order_statistics',
                    component: (resolve) => require(['@/components/agent/agent_order_statistics/index'], resolve),
                    children: [
                      {
                        path: '',
                        name: 'agent_order_statistics',
                        component: (resolve) => require(['@/components/agent/agent_order_statistics/order/index'], resolve)
                      },
                      {
                        path: ':id12(\\d+)',
                        name: 'detail',
                        component: (resolve) => require(['@/components/agent/agent_order_statistics/detail/index'], resolve)
                      },
                      {
                        path: 'order_incomes',
                        name: 'order_incomes',
                        component: (resolve) => require(['@/components/agent/agent_order_statistics/order_incomes/index'], resolve)
                      }
                    ]
                  }
               ]
            }
          ]
        }
      ]
    },
    {
      path: '/agent_increase',
      component: (resolve) => require(['@/layout/layout'], resolve),
      meta: { requireAuth: true },
      children: [
        {
          path: '',
          component: (resolve) => require(['@/components/agentPublicDetail/index'], resolve),
          children: [
            {
              path:'',
              component: (resolve) => require(['@/components/agentLayout/agentLayout'], resolve),
              children: [
                {
                  path: '',
                  name: 'index',
                  component: (resolve) => require(['@/components/agent/agent_profit/index'], resolve)
                },
                {
                  path: ':id7(\\D{3,4})',
                  name: 'agentform',
                  component: (resolve) => require(['@/components/agent/agent_profit/form'], resolve)
                }
              ]
            },
            {
              path:'agent_product_template',
              component: (resolve) => require(['@/components/agentLayout/agentLayout'], resolve),
              children: [
                {
                  path: '',
                  name: 'agent_product_template',
                  component: (resolve) => require(['@/components/agent/agent_product_template/index'], resolve) 
                },
                {
                  path: ':id8(\\D+)',
                  name: 'agentproductform',
                  component: (resolve) => require(['@/components/agent/agent_product_template/form'], resolve)
                } 
              ]
            }
          ]
        },
        {
          path: ':id9(\\d+)',
          name: '',
          component: (resolve) => require(['@/components/agent_increase/agent_increase_detail/index'], resolve),
          children: [
            {
              path: '/',
              name: 'index',
              component: (resolve) => require(['@/components/agent/agentShow/index'], resolve)
            },
            {
              path: 'terminal_products',
              component: (resolve) => require(['@/components/agent/agentDetailLayout/index'], resolve),
               children: [
                  {
                    path: '',
                    name: 'terminal_products',
                    component: (resolve) => require(['@/components/agent/terminal_products/index'], resolve)
                  },
                  {
                    path: ':id10(\\D{3,4})',
                    name: 'terminal_products/agentproductform',
                    component: (resolve) => require(['@/components/agent/agent_product_template/form'], resolve)
                  },
                  {
                    path: ':id11(\\D{5,})',
                    name: 'terminal_products/carrier_product_price',
                    component: (resolve) => require(['@/components/carrier_product/carrier_product_price'], resolve)
                  }
               ]
            },
            {
              path: 'sim_cards',
              component: (resolve) => require(['@/components/agent/agentDetailLayout/index'], resolve),
               children: [
                  {
                    path: '',
                    name: 'sim_cards',
                    component: (resolve) => require(['@/components/agent/sim_cards/index'], resolve)
                  },
                  {
                    path: ':id6(\\d+)',
                    name: '',
                    component: (resolve) => require(['@/components/sim_cards/show'], resolve),
                    children: [
                      {
                        path: '',
                        name: 'usagedetail',
                        component: (resolve) => require(['@/components/sim_cards/usage_detail'], resolve)
                      },
                      {
                        path: 'smsusagedetail',
                        name: 'smsusagedetail',
                        component: (resolve) => require(['@/components/sim_cards/sms_usage_detail'], resolve)
                      },
                      {
                        path: 'realnamecertification',
                        name: 'realnamecertification',
                        component: (resolve) => require(['@/components/sim_cards/real_name_certification'], resolve)
                      },
                      {
                        path: 'historyevent',
                        name: 'historyevent',
                        component: (resolve) => require(['@/components/sim_cards/history_event'], resolve)
                      },{
                        path: 'package',
                        name: 'package',
                        component: (resolve) => require(['@/components/sim_cards/package'], resolve)
                      }
                    ]
                  }
               ]
            },
            {
              path: 'traffic_pools',
              component: (resolve) => require(['@/components/agent/agentDetailLayout/index'], resolve),
               children: [
                {
                  path: '',
                  name: 'traffic_pools',
                  component: (resolve) => require(['@/components/agent_increase/traffic_pools/index'], resolve)
                },
                {
                  path: 'edit',
                  name: 'edit',
                  component: (resolve) => require(['@/components/agent_increase/traffic_pools/form'], resolve)
                },
                {
                  path: ':id20(\\d+)',
                  name: 'trafficpoolsdetail',
                  component: (resolve) => require(['@/components/agent_increase/traffic_pools/detail'], resolve),
                  children: [
                    {
                      path: '',
                      name: 'pool_cards',
                      component: (resolve) => require(['@/components/agent_increase/pool_cards/index'], resolve)
                    },
                    {
                      path: 'pool_histories',
                      name: 'pool_histories',
                      component: (resolve) => require(['@/components/agent_increase/pool_histories/index'], resolve)
                    },
                    {
                      path: 'pool_financial_statistics',
                      name: 'pool_financial_statistics',
                      component: (resolve) => require(['@/components/agent_increase/pool_financial_statistics/index'], resolve)
                    }
                  ]
                }
               ]
            },
            {
              path: 'agent_service_circle_manage',
              component: (resolve) => require(['@/components/agent/agentDetailLayout/index'], resolve),
               children: [
                {
                  path: '',
                  name: 'agent_service_circle_manage',
                  component: (resolve) => require(['@/components/agent_increase/agent_service_circle_manage/index'], resolve)
                },
                {
                  path: ':id13(\\d+)',
                  name: 'agent_service_circle_manage_detail',
                  component: (resolve) => require(['@/components/agent_increase/agent_service_circle_manage/detail'], resolve)
                }
               ]
            },
            {
              path: 'agent_order_statistics',
              component: (resolve) => require(['@/components/agent/agentDetailLayout/index'], resolve),
               children: [
                  {
                    path: '',
                    name: 'agent_order_statistics',
                    component: (resolve) => require(['@/components/agent_increase/agent_increase_order_statistics/index'], resolve),
                    children: [
                      {
                        path: '',
                        name: 'agent_order_statistics',
                        component: (resolve) => require(['@/components/agent_increase/agent_increase_order_statistics/order/index'], resolve)
                      },
                      {
                        path: ':id12(\\d+)',
                        name: 'agentincreasedetail',
                        component: (resolve) => require(['@/components/agent_increase/agent_increase_order_statistics/detail/index'], resolve)
                      },
                      {
                        path: 'order_incomes',
                        name: 'order_incomes',
                        component: (resolve) => require(['@/components/agent/agent_order_statistics/order_incomes/index'], resolve)
                      },
                      {
                        path: 'company_agent_orders',
                        name: 'company_agent_orders',
                        component: (resolve) => require(['@/components/agent_increase/agent_increase_order_statistics/company_agent_orders/index'], resolve)
                      }
                    ]
                  }
               ]
            },
            {
              path: 'agent_flow_statistics',
              component: (resolve) => require(['@/components/agent/agentDetailLayout/index'], resolve),
               children: [
                {
                  path: '',
                  name: 'agent_flow_statistics',
                  component: (resolve) => require(['@/components/agent_increase/agent_flow_statistics/index'], resolve)
                },
                {
                  path: ':id13(\\d+)',
                  name: 'detail',
                  component: (resolve) => require(['@/components/agent_increase/agent_flow_statistics/detail'], resolve)
                },
               ]
            }
          ]
        }
      ]
    },
    {
      path: '/batch_operation_records', 
      redirect: '/batch_operation_records',
      component: (resolve) => require(['@/layout/layout'], resolve),
      meta: { requireAuth: true },
      children: [
        {
          path: '',
          name: '批量操作查询',
          component: (resolve) => require(['@/components/work_item/batch_operation_records/index'], resolve)
        },
        {
          path: ':id(\\d+)',
          name: 'detail',
          component: (resolve) => require(['@/components/work_item/batch_operation_records/detail'], resolve)
        }
      ]
    },
    {
      path: '/authentication_records', 
      redirect: '/authentication_records',
      component: (resolve) => require(['@/layout/layout'], resolve),
      meta: { requireAuth: true },
      children: [
        {
          path: '',
          name: '批量操作查询',
          component: (resolve) => require(['@/components/work_item/authentication_records/index'], resolve)
        },
        {
          path: ':id(\\d+)',
          name: '',
          component: (resolve) => require(['@/components/work_item/authentication_records/detail'], resolve)
        }
      ]
    },
    {
      path: '/announcements', 
      redirect: '/announcements',
      component: (resolve) => require(['@/layout/layout'], resolve),
      meta: { requireAuth: true },
      children: [
        {
          path: '',
          name: '公告管理',
          component: (resolve) => require(['@/components/work_item/announcements/index'], resolve)
        },
        {
          path: ':id1(\\D+)',
          name: '',
          component: (resolve) => require(['@/components/work_item/announcements/form'], resolve)
        },
        {
          path: ':id1(\\d+)',
          name: '',
          component: (resolve) => require(['@/components/work_item/announcements/detail'], resolve)
        }
      ]
    },
    {
      path: '/cancel_card_manages', 
      redirect: '/cancel_card_manages',
      component: (resolve) => require(['@/layout/layout'], resolve),
      meta: { requireAuth: true },
      children: [
        {
          path: '',
          name: '销卡管理',
          component: (resolve) => require(['@/components/work_item/cancel_card_manages/index'], resolve)
        }
      ]
    },
    { path: '/users', 
      redirect: '/users',
      component: (resolve) => require(['@/layout/layout'], resolve),
      meta: { requireAuth: true },
      children: [
        {
          path: '',
          component: (resolve) => require(['@/components/users/index'], resolve),
          children: [
            {
              path:'',
              component: (resolve) => require(['@/components/agentLayout/agentLayout'], resolve),
              children: [
                {
                  path: '',
                  name: 'admin',
                  component: (resolve) => require(['@/components/users/admin/index'], resolve)
                },
                {
                  path: ':id30(\\D{3,4})',
                  name: 'admin_form',
                  component: (resolve) => require(['@/components/users/admin/form'], resolve)
                },
                {
                  path: ':id(\\d+)',
                  name: 'admin_detail',
                  component: (resolve) => require(['@/components/users/admin/detail'], resolve)
                }
              ]
            },
            {
              path:'sales',
              component: (resolve) => require(['@/components/agentLayout/agentLayout'], resolve),
              children: [
                {
                  path: '',
                  name: 'sales',
                  component: (resolve) => require(['@/components/users/sales/index'], resolve)
                },
                {
                  path: ':id(\\D{3,4})',
                  name: 'sales_form',
                  component: (resolve) => require(['@/components/users/sales/form'], resolve)
                }
              ]
            },
            {
              path:'customer_service',
              component: (resolve) => require(['@/components/agentLayout/agentLayout'], resolve),
              children: [
                {
                  path: '',
                  name: 'customer_service',
                  component: (resolve) => require(['@/components/users/customer_service/index'], resolve)
                },
                {
                  path: ':id(\\D{3,4})',
                  name: 'customer_service_form',
                  component: (resolve) => require(['@/components/users/customer_service/form'], resolve)
                }
              ]
            },
            {
              path:'finance',
              component: (resolve) => require(['@/components/agentLayout/agentLayout'], resolve),
              children: [
                {
                  path: '',
                  name: 'finance',
                  component: (resolve) => require(['@/components/users/finance/index'], resolve)
                },
                {
                  path: ':id(\\D{3,4})',
                  name: 'finance_form',
                  component: (resolve) => require(['@/components/users/finance/form'], resolve)
                }
              ]
            }
          ]
        }
      ]
    },
    {
      path: '/team', 
      redirect: '/team',
      component: (resolve) => require(['@/layout/layout'], resolve),
      meta: { requireAuth: true },
      children: [
        {
          path: '',
          name: '销售团队管理',
          component: (resolve) => require(['@/components/users/team/index'], resolve)
        },
        {
          path: ':id(\\D+)',
          name: 'team_form',
          component: (resolve) => require(['@/components/users/team/form'], resolve)
        }
      ]
    },
    {
      path: '/user_business_types', 
      redirect: '/user_business_types',
      component: (resolve) => require(['@/layout/layout'], resolve),
      meta: { requireAuth: true },
      children: [
        {
          path: '',
          name: '用户业务模式管理',
          component: (resolve) => require(['@/components/users/user_business_types/index'], resolve)
        },
        {
          path: ':id(\\D+)',
          name: 'user_business_types_form',
          component: (resolve) => require(['@/components/users/user_business_types/form'], resolve)
        }
      ]
    },
    {
      path: '/platform_wechats', 
      redirect: '/platform_wechats',
      component: (resolve) => require(['@/layout/layout'], resolve),
      meta: { requireAuth: true },
      children: [
        {
          path: '',
          name: '微信公众号',
          component: (resolve) => require(['@/components/users/platform_wechats/index'], resolve)
        }
      ]
    },
    {
      path: '/api_interfaces', 
      redirect: '/api_interfaces',
      component: (resolve) => require(['@/layout/layout'], resolve),
      meta: { requireAuth: true },
      children: [
        {
          path: '',
          name: '平台接口调用查询',
          component: (resolve) => require(['@/components/setting/api_interfaces/index'], resolve)
        },
        {
          path: 'edit',
          name: '编辑',
          component: (resolve) => require(['@/components/setting/api_interfaces/form'], resolve)
        }
      ]
    },
    {
      path: '/carrier_interface_sets', 
      redirect: '/carrier_interface_sets',
      component: (resolve) => require(['@/layout/layout'], resolve),
      meta: { requireAuth: true },
      children: [
        {
          path: '',
          name: '运营商接口管理',
          component: (resolve) => require(['@/components/setting/carrier_interface_sets/index'], resolve)
        },
        {
          path: ':id(\\D{3,4})',
          name: '编辑',
          component: (resolve) => require(['@/components/setting/carrier_interface_sets/form'], resolve)
        },
        {
          path: ':id(\\d+)',
          name: 'detail',
          component: (resolve) => require(['@/components/setting/carrier_interface_sets/detail'], resolve),
          children: [
            {
              path: '',
              name: 'index',
              component: (resolve) => require(['@/components/setting/carrier_interface_sets/detail/index'], resolve)
            },
            {
              path: 'new',
              name: 'new',
              component: (resolve) => require(['@/components/setting/carrier_interface_sets/detail/form'], resolve)
            }
          ]
        }
      ]
    },
    {
      path: '/sim_card_carrier_tasks', 
      redirect: '/sim_card_carrier_tasks',
      component: (resolve) => require(['@/layout/layout'], resolve),
      meta: { requireAuth: true },
      children: [
        {
          path: '',
          component: (resolve) => require(['@/components/setting/sim_card_carrier_tasks/detail'], resolve),
          children: [
            {
              path: '',
              name: 'index',
              component: (resolve) => require(['@/components/setting/sim_card_carrier_tasks/sim_card_carrier_tasks/index'], resolve)
            },
            {
              path: 'api_interface_call_histories',
              name: 'api_interface_call_histories',
              component: (resolve) => require(['@/components/setting/sim_card_carrier_tasks/api_interface_call_histories/index'], resolve)
            }
          ]
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
    if (to.matched.some(res => res.meta.requireAuth)) { // 判断是否需要登录权限
        if (window.localStorage.getItem('token')) { // 判断是否登录
            next()
        } else { // 没登录则跳转到登录界面
            next({
                path: '/'
            })
        }
    } else {
        next()
    }
})

export default router